package com.honey.solr.daosupport.method;

import java.util.Iterator;
import java.util.Map;

import com.honey.compilation.xml.XmlElement;
import com.honey.core.notify.SpringSignal;
import com.honey.solr.SolrConstant;
import com.honey.solr.SolrFactory;

public class SpringMethod {
	
	private SolrFactory solrFactory ;
	Client  client = null ;
	public SpringMethod( SolrFactory solrFactory ){
		this.solrFactory = solrFactory;
		this.client = new Client3(); 
		
	}
	
	public void snedBeanSignalToSpringPlugin(){
		SpringSignal springSignal = new SpringSignal();
		springSignal.setSpringContextFile(solrFactory.getSolrPlugin().getSolrSpringContextFile());
		
		XmlElement xml = client.getHttpConnectionParams();
		springSignal.setAttachment(xml);
		solrFactory.snedSignal(springSignal);
		
		xml = client.getHttpConnectionManager();
		springSignal.setAttachment(xml);
		solrFactory.snedSignal(springSignal);

		
		xml = client.getHttpClient();
		springSignal.setAttachment(xml);
		solrFactory.snedSignal(springSignal);

		
		Map<String, String> solrServers = solrFactory.getSolrPlugin().getSolrServers() ;
		Iterator<String> it = solrServers.keySet().iterator();
		while( it.hasNext() ){
			String name = it.next();
			String url =solrServers.get(name);
			xml = client.getSolrServer(name, url);
			springSignal.setAttachment(xml);
			solrFactory.snedSignal(springSignal);
			
			xml = client.getSolrDaoSupport(name , url);
			springSignal.setAttachment(xml);
			solrFactory.snedSignal(springSignal);

		}
	}
	
	
	private interface Client{
		
		public XmlElement getHttpConnectionParams();
		
		public XmlElement getHttpConnectionManager();
		
		public XmlElement getHttpClient();
		
		public XmlElement getSolrServer(String name , String url );
		
		public XmlElement getSolrDaoSupport(String name , String url);
		
	}
	
	private class Client3 implements Client {

		@Override
		public XmlElement getHttpConnectionParams() {
			XmlElement answer =new XmlElement("bean");
			answer.addXmlDoc("httpclient连接参数");
			answer.addAttribute("id", "httpConnectionParams");
			answer.addAttribute("class", "org.apache.commons.httpclient.params.HttpConnectionManagerParams");
			answer.addElement(new XmlElement("property").addAttribute("name", "connectionTimeout").addAttribute("value","10000"));
			answer.addElement(new XmlElement("property").addAttribute("name", "soTimeout").addAttribute("value","20000"));
			answer.addElement(new XmlElement("property").addAttribute("name", "maxTotalConnections").addAttribute("value","128"));
			answer.addElement(new XmlElement("property").addAttribute("name", "defaultMaxConnectionsPerHost").addAttribute("value","64"));
			answer.addElement(new XmlElement("property").addAttribute("name", "contentCharset").addAttribute("value","UTF-8"));
			return answer;
		}

		@Override
		public XmlElement getHttpConnectionManager() {
			XmlElement answer =new XmlElement("bean");
			answer.addXmlDoc("创建httpclient连接池");
			answer.addAttribute("id", "httpConnectionManager");
			answer.addAttribute("class", "org.apache.commons.httpclient.MultiThreadedHttpConnectionManager");
			answer.addElement(new XmlElement("property").addAttribute("name", "params").addAttribute("ref","httpConnectionParams"));
			answer.addElement(new XmlElement("property").addAttribute("name", "sendBufferSize").addAttribute("value","512"));
			return answer;
		}

		@Override
		public XmlElement getHttpClient() {
			XmlElement answer =new XmlElement("bean");
			answer.addXmlDoc("创建httpclient连接对象");
			answer.addAttribute("id", "httpClient");
			answer.addAttribute("class", "org.apache.commons.httpclient.HttpClient");
			answer.addElement(new XmlElement("constructor-arg").addElement(new XmlElement("ref").addAttribute("bean", "httpConnectionManager")));
			return answer;
		}
		
		@Override
		public XmlElement getSolrServer(String name , String url) {
			XmlElement answer =new XmlElement("bean");
			answer.addXmlDoc( name + " Solr Session");
			//answer.addAttribute("id", SolrConstant.SOLR_SERVER_FIELD_NAME);
			answer.addAttribute("id", name);
			answer.addAttribute("class", "org.apache.solr.client.solrj.impl.CommonsHttpSolrServer");
			answer.addElement(new XmlElement("constructor-arg").addAttribute("index", "0").addAttribute("value", url));
			answer.addElement(new XmlElement("constructor-arg").addAttribute("index", "1").addElement(new XmlElement("ref").addAttribute("bean", "httpConnectionManager")));
			return answer;
		}

		@Override
		public XmlElement getSolrDaoSupport(String name , String url) {
			XmlElement answer =new XmlElement("bean");
			answer.addXmlDoc(name+ " dao support");
			//answer.addAttribute("id", "solrDaoSupport");
			answer.addAttribute("id", name + "DaoSupport");
			answer.addAttribute("class", solrFactory.getSolrPlugin().getTargetPackage()+"."+ solrFactory.getSolrPlugin().getTargetClassName());
			answer.addElement(new XmlElement("property").addAttribute("name", SolrConstant.SOLR_SERVER_FIELD_NAME).addAttribute("ref",name));
			answer.addElement(new XmlElement("property").addAttribute("name", "autoCommit").addAttribute("value","true"));
			answer.addElement(new XmlElement("property").addAttribute("name", "highlight").addAttribute("value",String.valueOf( solrFactory.getSolrPlugin().isSupporHighlight())));
			answer.addElement(new XmlElement("property").addAttribute("name", "highlightSimplePre").addAttribute("value","<font color=\"red\">"));
			answer.addElement(new XmlElement("property").addAttribute("name", "highlightSimplePost").addAttribute("value","</font>"));
			return answer;
		}
	}
	
	
	
	
	
}
